package com.it.project.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.it.common.utils.SecurityUtils;
import com.it.project.mapper.UserProjectMapper;
import com.it.project.domain.UserProject;
import com.it.project.service.IUserProjectService;

/**
 * 用户项目关联Service业务层处理
 * 
 * @author it
 * @date 2024-07-15
 */
@Service
public class UserProjectServiceImpl implements IUserProjectService
{
    @Autowired
    private UserProjectMapper userProjectMapper;

    /**
     * 查询用户项目关联
     * 
     * @param id 用户项目关联主键
     * @return 用户项目关联
     */
    @Override
    public UserProject selectUserProjectById(Long id)
    {
        return userProjectMapper.selectUserProjectById(id);
    }

    /**
     * 查询用户项目关联列表
     * 
     * @param userProject 用户项目关联
     * @return 用户项目关联集合
     */
    @Override
    public List<UserProject> selectUserProjectList(UserProject userProject)
    {
        return userProjectMapper.selectUserProjectList(userProject);
    }

    /**
     * 新增用户项目关联
     * 
     * @param userProject 用户项目关联
     * @return 结果
     */
    @Override
    public int insertUserProject(UserProject userProject)
    {
        userProject.setCreateBy(SecurityUtils.getUsername());
        return userProjectMapper.insertUserProject(userProject);
    }

    /**
     * 修改用户项目关联
     * 
     * @param userProject 用户项目关联
     * @return 结果
     */
    @Override
    public int updateUserProject(UserProject userProject)
    {
        userProject.setUpdateBy(SecurityUtils.getUsername());
        return userProjectMapper.updateUserProject(userProject);
    }

    /**
     * 批量删除用户项目关联
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteUserProjectByIds(Long[] ids)
    {
        return userProjectMapper.deleteUserProjectByIds(ids);
    }

    /**
     * 删除用户项目关联信息
     * 
     * @param id 用户项目关联主键
     * @return 结果
     */
    @Override
    public int deleteUserProjectById(Long id)
    {
        return userProjectMapper.deleteUserProjectById(id);
    }

    /**
     * 检查用户是否已有项目权限
     * 
     * @param userProject 用户ID和项目ID
     * @return 结果
     */
    @Override
    public boolean checkUserProjectExists(UserProject userProject)
    {
        UserProject existing = userProjectMapper.selectUserProjectByUserIdAndProId(userProject);
        return existing != null;
    }

    /**
     * 根据用户ID查询项目列表
     * 
     * @param userId 用户ID
     * @return 项目集合
     */
    @Override
    public List<UserProject> selectProjectsByUserId(Long userId)
    {
        return userProjectMapper.selectProjectsByUserId(userId);
    }

    /**
     * 根据项目ID查询用户列表
     * 
     * @param proId 项目ID
     * @return 用户集合
     */
    @Override
    public List<UserProject> selectUsersByProId(Long proId)
    {
        return userProjectMapper.selectUsersByProId(proId);
    }

    /**
     * 校验用户是否有指定项目的权限
     * 
     * @param userId 用户ID
     * @param proId 项目ID
     * @return 结果
     */
    @Override
    public boolean checkUserProjectPermission(Long userId, Long proId)
    {
        UserProject userProject = new UserProject();
        userProject.setUserId(userId);
        userProject.setProId(proId);
        userProject.setStatus(1); // 只检查启用状态的关联
        
        UserProject existing = userProjectMapper.selectUserProjectByUserIdAndProId(userProject);
        return existing != null;
    }

    /**
     * 获取用户在项目中的权限等级
     * 
     * @param userId 用户ID
     * @param proId 项目ID
     * @return 权限等级
     */
    @Override
    public Integer getUserProjectPermissionLevel(Long userId, Long proId)
    {
        UserProject userProject = new UserProject();
        userProject.setUserId(userId);
        userProject.setProId(proId);
        userProject.setStatus(1); // 只检查启用状态的关联
        
        UserProject existing = userProjectMapper.selectUserProjectByUserIdAndProId(userProject);
        return existing != null ? existing.getPermissionLevel() : null;
    }
}